package com.example.thread;

import cn.hutool.core.thread.ThreadUtil;
import com.google.common.base.Throwables;
import lombok.SneakyThrows;
import org.junit.Test;

import javax.xml.transform.Source;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutorService;

/**
 * thread
 *
 * @author qingyan 2022-01-21
 */
public class ThreadTest {
    @Test
    public void test() {
        Runnable run = () -> {
            int i = 1 / 0;
            System.out.println(Thread.currentThread().getName());
        };

        Thread thread = new Thread(run);
        thread.start();
        try {
            Thread.sleep(10);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    @SneakyThrows
    @Test
    public void test1() {
        ExecutorService executorService = ThreadUtil.newExecutor(1);
        executorService.execute(() -> {
            int i = 1 / 0;
            System.out.println(Thread.currentThread().getName());
        });
        Thread.sleep(1000);

    }

    @SneakyThrows
    @Test
    public void test4() {

        CompletableFuture.supplyAsync(() -> {
            System.out.println(Thread.currentThread().getName());
            int i = 1 / 0;
            return i;
        }).whenComplete((integer, throwable) -> {
            System.err.println(Throwables.getStackTraceAsString(throwable));
        });
    }

}
